package com.homedo.odin.solution.service.bizservice.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.contract.mng.DictDataReq;
import com.homedo.odin.solution.contract.mng.DictDataResp;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.model.entity.DictData;
import com.homedo.odin.solution.service.DictDataServiceImpl;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by liyi on 2018/5/21.
 */
@Service
public class MngDictDataBizService {

    @Autowired
    private DictDataServiceImpl dictDataService;

    public void updateDictData(DictDataReq dictDataReq) {
        DictData addDictData = new DictData();
        DictData dictData = dictDataService.getDictDataByTypeAndKey(dictDataReq.getType(),dictDataReq.getKey());
        //判断是否存在
        if (dictData!=null) {
            DictData updateDictData = dictData;
            updateDictData.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
            updateDictData.setKey(dictDataReq.getKey());
            updateDictData.setModifiedBy(dictDataReq.getModifiedBy());
            updateDictData.setModifiedName(dictDataReq.getModifiedName());
            updateDictData.setModifiedTime(new Date());
            updateDictData.setType(dictDataReq.getType());
            updateDictData.setValue(dictDataReq.getValue());
            dictDataService.updateById(updateDictData);
        } else {
            addDictData.setCreatedBy(dictDataReq.getModifiedBy());
            addDictData.setCreatedTime(new Date());
            addDictData.setCreatedName(dictDataReq.getModifiedName());
            addDictData.setId(dictDataReq.getId());
            addDictData.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
            addDictData.setKey(dictDataReq.getKey());
            addDictData.setModifiedBy(dictDataReq.getModifiedBy());
            addDictData.setModifiedName(dictDataReq.getModifiedName());
            addDictData.setModifiedTime(new Date());
            addDictData.setType(dictDataReq.getType());
            addDictData.setValue(dictDataReq.getValue());
            dictDataService.insert(addDictData);
        }

    }

    public DictDataResp getDictDataByTypeAndKey(String type, String key) {
        if (StringUtils.isBlank(type)) {
            throw new BizException(Meta.CODE_451.getCode(), "该类型异常");
        }
        if (StringUtils.isBlank(key)) {
            throw new BizException(Meta.CODE_451.getCode(), "该关键字异常");
        }
        DictData dictData = dictDataService.getDictDataByTypeAndKey(type, key);
        if (dictData == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "该数据不存在");
        }
        DictDataResp resp = BeanUtils.transfrom(DictDataResp.class, dictData);
        return resp;
    }
}
